package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.QueryUserDTO;
import com.woniu.entity.dto.UserLoginDTO;
import com.woniu.entity.vo.OrderByIdVO;
import com.woniu.entity.vo.UserDetailedStatisticsVO;
import com.woniu.mapper.UserMapper;
import com.woniu.pojo.User;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;

/**
* @author Ma.Lei
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2022-12-16 23:28:28
*/

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public User getByPhone(String phone) {
        return userMapper.getByPhone(phone);
    }

    @Override
    public void register(UserLoginDTO loginUserDTO) {
        String newPass = new SimpleHash("MD5", loginUserDTO.getPassword(), null).toString();
        User user = new User();
        user.setPhone(loginUserDTO.getPhone());
        user.setPassword(newPass);
        System.out.println(newPass);
        userMapper.insert(user);
    }

    @Override
    public void setLoginTime(String phone) {
//        Date time = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String loginTime = simpleDateFormat.format(time);
        userMapper.setLoginTime(phone,new Date());
    }

    @Override
    public User checkOldPwd(String userId, String oldPwd) {
        return userMapper.checkOldPwd(userId,oldPwd);
    }

    @Override
    public boolean updatePwd(String userId, String newPwd) {
        return userMapper.updatePwd(userId,newPwd);
    }

    @Override
    public PageData listSearch(Integer currentPage, Integer pageSize, QueryUserDTO queryUserDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (queryUserDTO != null) {
            queryWrapper.like((!StringUtils.isEmpty(queryUserDTO.getUserName())), "user_name", queryUserDTO.getUserName());
            queryWrapper.like(queryUserDTO.getPhone() != null, "phone", queryUserDTO.getPhone());
            queryWrapper.eq(queryUserDTO.getStatus() != null, "status", queryUserDTO.getStatus());
        }
        IPage<User> page = new Page<>(currentPage,pageSize);
        IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
        return new PageData(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public int disabledUser(String id,Integer status) {
        int disabledUser = userMapper.disabledUser(id,status);
        return disabledUser;
    }

    @Override
    public List<OrderByIdVO> getOrderRecordByUserId(String id) {
        List<OrderByIdVO> userOrdeer=userMapper.getOrderRecordByUserId(id);
        return userOrdeer;
    }

    @Override
    public UserDetailedStatisticsVO getDetailedStatisticsByUserId(String id) {
        return userMapper.UserDetailedStatisticsById(id);
    }
}





